package com.magnificent.panda.ui.dialog.panels;

import com.silverhaze.commons.ui.dialogs.DialogFactory;
import com.silverhaze.commons.ui.table.annotation.AnnotationTable;
import com.silverhaze.commons.ui.table.annotation.AnnotationTableModel;
import com.silverhaze.commons.ui.table.annotation.AnnotationValue;
import com.silverhaze.commons.ui.table.annotation.UITableColumn;
import com.silverhaze.commons.utils.collections.list.MetaArrayList;
import com.silverhaze.commons.utils.collections.list.MetaList;
import com.silverhaze.commons.utils.collections.list.functions.SelectWithIndexFunction;
import com.silverhaze.commons.helpers.CommonUIHelper;
import com.magnificent.panda.model.entity.FlowEvent;
import com.magnificent.panda.ui.dialog.ActorSettingsDialog;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * User: Babanin
 * Date: 06.03.2010
 * Time: 9:09:25
 */
public class ValuePanel extends JPanel {
    private AnnotationTable<Value> valuesTable;
    private AnnotationTableModel<Value> valuesTableModel;

    private JTextField valueTextField;

    private JButton addButton;
    private JButton removeButton;

    private int maxValue;
    private List<FlowEvent> values;

    private ActorSettingsDialog dialog;

    public ValuePanel(ActorSettingsDialog dialog, List<FlowEvent> values) {
        this.values = values;
        this.dialog = dialog;

        initializeControls();
        initializeView();
    }

    private void initializeView() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        add(new JScrollPane(valuesTable), BorderLayout.CENTER);
        add(CommonUIHelper.packInShiftedPanel(CommonUIHelper.hBox(valueTextField, 2, addButton, 2, removeButton), 0, 5, 0, 0), BorderLayout.NORTH);
    }

    private void initializeControls() {
        valuesTableModel = new AnnotationTableModel<Value>(Value.class, getValues(values));

        valuesTable = new AnnotationTable<Value>(valuesTableModel);
        valueTextField = new JTextField();

        valuesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (valuesTable.getSelectedRow() >= 0) {
                    valueTextField.setText("" + valuesTableModel.getValue(valuesTable.getSelectedRow()).value);
                }
            }
        });

        addButton = new JButton(new AbstractAction("Добавить") {
            public void actionPerformed(ActionEvent e) {
                int newValue;

                try {
                    newValue = Integer.parseInt(valueTextField.getText());
                } catch (NumberFormatException nfe) {
                    DialogFactory.error("Необходимо ввести корректное значение", dialog);
                    return;
                }

                dialog.addFirstValue(newValue);

                valueTextField.setText("");
            }
        });

        removeButton = new JButton(new AbstractAction("Удалить") {
            public void actionPerformed(ActionEvent e) {
                int newValue;

                try {
                    newValue = Integer.parseInt(valueTextField.getText());
                } catch (NumberFormatException nfe) {
                    DialogFactory.error("Необходимо ввести корректное значение", dialog);
                    return;
                }

                dialog.removeFirstValue(newValue);

                valueTextField.setText("");
            }
        });
    }

    public MetaList<Value> getValues(List<FlowEvent> values) {
        return new MetaArrayList<FlowEvent>(values).<FlowEvent>orderBy().selectWithIndex(new SelectWithIndexFunction<FlowEvent, Value>() {
            public Value select(FlowEvent comparable, int index) {
                Value value = new Value(comparable.getValue());
                value.setId(index + 1);

                return value;
            }
        });
    }


    public void updateValues(List<FlowEvent> values) {
        this.values = values;

        valuesTableModel = new AnnotationTableModel<Value>(Value.class, getValues(values));
        valuesTable.setModel(valuesTableModel);
        valuesTable.updateColumnSize();
    }

    public void updateMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    public class Value implements AnnotationValue<Long> {

        private int id;
        private Long value;

        private Value(Long value) {
            this.value = value;
        }

        @UITableColumn(
                name = "id",
                userFriendlyName = "#",
                order = 1,
                columnMinWidth = 40,
                columnMaxWidth = 40
        )
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }


        @UITableColumn(
                name = "value",
                userFriendlyName = "Значение",
                order = 2
        )
        public Long getValue() {
            return value;
        }

        public void setValue(Long value) {
            this.value = value;
        }
    }
}

  /* converted to utf8 */